﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using whatpricemyhouse.LINQ;
using System.Linq;

namespace whatpricemyhouse.Admin
{
    public partial class EditUsers : System.Web.UI.Page
    {
        string userName = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            // retrieve the username from the querystring
            userName = this.Request.QueryString["UserName"];

            lblRolesOK.Visible = false;
            lblProfileOK.Visible = false;

            //UserProfile1.UserName = userName;
            MembershipUser user = Membership.GetUser(userName);
            String userID = user.ProviderUserKey.ToString();
            lblUserName.Text = user.UserName;
            lnkEmailAddress.Text = user.Email;
            lnkEmailAddress.NavigateUrl = "mailto:" + user.Email;
            lblRegistered.Text = user.CreationDate.ToString("f");
            lblLastLogin.Text = user.LastLoginDate.ToString("f");
            lblLastActivity.Text = user.LastActivityDate.ToString("f");
            chkIsLockedOut.Checked = user.IsLockedOut;
            chkIsLockedOut.Enabled = user.IsLockedOut;
            chkIsOnlineNow.Checked = user.IsOnline;
            chkIsApproved.Checked = user.IsApproved;
            BindRoles();
            DataControllerDataContext dcdc = new DataControllerDataContext();
            var agents = from a in dcdc.place_agent_tables
                         where a.UserID == new Guid(userID)
                         select a;
            foreach (var agent in agents)
            {
                RegionLabel.Text = agent.suburb.area;
                PlaceLabel.Text = agent.suburb.suburb1;
                PostcodeLabel.Text = agent.suburb.postcode;
                break;
            }
        }

        private void BindRoles()
        {
            chklRoles.DataSource = Roles.GetAllRoles();
            chklRoles.DataBind();
            foreach (string role in Roles.GetRolesForUser(userName))
                chklRoles.Items.FindByText(role).Selected = true;
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            // first remove the user from all roles...
            string[] currentRoles = Roles.GetRolesForUser(userName);

            if (currentRoles.Length > 0)
                Roles.RemoveUserFromRoles(userName, currentRoles);
            // and then add the user to the selected roles
            List<string> newRoles = new List<string>();
            foreach (ListItem item in chklRoles.Items)
            {
                if (item.Selected)
                    newRoles.Add(item.Text);
            }
            if (newRoles.Count == 0)
            {
                Roles.AddUserToRole(userName, "Agent");
            }
            else { 
                Roles.AddUserToRoles(userName, newRoles.ToArray());
            }
            lblRolesOK.Visible = true;
        }

        protected void chkIsApproved_CheckedChanged(object sender, EventArgs e)
        {
            MembershipUser user = Membership.GetUser(userName);
            user.IsApproved = chkIsApproved.Checked;
            Membership.UpdateUser(user);
        }
        protected void chkIsLockedOut_CheckedChanged(object sender, EventArgs e)
        {
            if (!chkIsLockedOut.Checked)
            {
                MembershipUser user = Membership.GetUser(userName);
                user.UnlockUser();
                chkIsLockedOut.Enabled = false;
            }
        }

        protected void btnUpdateProfile_Click(object sender, EventArgs e)
        {
            //UserProfile1.Save();
            MembershipUser user = Membership.GetUser(userName);
            String userID = user.ProviderUserKey.ToString();
            DataControllerDataContext dcdc = new DataControllerDataContext();
            int i = 0;
            int placeId = 625;
            try
            {
                placeId = Int16.Parse(PlaceSelector.SelectedValue);
            }
            catch (Exception) { }
            //check agent number in selected place
            var placeOfAgents = from poa in dcdc.place_agent_tables
                                where poa.suburbid == placeId
                                select poa;
            foreach (var agent in placeOfAgents) {
                i++;
            }
            if (i > 2)
            {
                lblProfileOK.Visible = true;
                lblProfileOK.Text = "Over 3 agents in " + PlaceSelector.SelectedItem.Text;
                return;
            }

            var agents = from a in dcdc.place_agent_tables
                         where a.UserID == new Guid(userID)
                         select a;

            i = 0;
            foreach (var agent in agents)
            {
                i++;
                agent.suburbid = placeId;
                break;
            }

            if (i == 0)
            {
                agent_info_table ait = new agent_info_table();
                ait.UserID = new Guid(userID) ;
                ait.address = "New Agent";
                ait.FirstName = "First Name";
                ait.IsShowEmail = true;
                dcdc.agent_info_tables.InsertOnSubmit(ait);

                place_agent_table pat = new place_agent_table();
                pat.suburbid = placeId;
                pat.UserID = new Guid(userID);
                dcdc.place_agent_tables.InsertOnSubmit(pat);
            }
            dcdc.SubmitChanges();

            Page_Load(sender, e);
            GridView1.DataBind();
            // lblProfileOK.Visible = true;
        }
    }
}